package com.tedu.weibo.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tedu.weibo.mapper.IUserMapper;
import com.tedu.weibo.mapper.IWeiboMapper;
import com.tedu.weibo.pojo.dto.WeiboDTO;
import com.tedu.weibo.pojo.entity.User;
import com.tedu.weibo.pojo.entity.Weibo;
import com.tedu.weibo.pojo.vo.WeiboDetailVO;
import com.tedu.weibo.pojo.vo.WeiboVO;
import com.tedu.weibo.result.JsonResult;
import com.tedu.weibo.result.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpSession;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
////@PathVariable:从地址中获取参数

//@Controller
@RestController
@RequestMapping("v1/weibo")
@Slf4j
@Api(tags = "微博模块")
@Validated
public class WeiboController {
    @Autowired
    private RedisTemplate<String, Serializable> redisTemplate;

    @Autowired
    private IWeiboMapper iWeiboMapper;

    //若前端是json格式的请求，那么在响应方法的参数上需要添加@RequestBody注解
    @ApiOperation("发表微博")
    @PostMapping("/insert")
    public JsonResult insert(@RequestBody WeiboDTO weiboDTO, @ApiIgnore HttpSession session){
        log.info("发表微博");
        log.trace("开始执行insert方法");
        User user = (User)session.getAttribute("user");
        log.trace("通过session获取用户登录信息");
        log.debug("user:"+user);

        if (user==null){
            log.trace("user为null,未登录");
            System.out.println("未登录");
            return new JsonResult(StatusCode.NOT_LOGIN);
        }else {
            log.trace("user不为null，处理发布微博");
            Weibo weibo = new Weibo();
            weibo.setContent(weiboDTO.getContent());
            weibo.setUserId(user.getId());
            weibo.setCreated(new Date());
            log.debug("weibo"+weibo);
            int rows = iWeiboMapper.insert(weibo);
            log.trace("插入微博执行完毕");

            return JsonResult.success();
        }

    }

    @ApiOperation("查看微博列表")
    @GetMapping("/selectIndex")
    public JsonResult selectIndex() throws JsonProcessingException {

        String weiboListStr =(String) redisTemplate.opsForValue().get("weiboList");//方法返回的是一个 Object 类型的对象
        if(weiboListStr==null){
            List<WeiboVO> weiboVOS = iWeiboMapper.selectAll();
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonString = objectMapper.writeValueAsString(weiboVOS);//该方法可以将给定的对象转换为json格式
            redisTemplate.opsForValue().set("weiboList",jsonString);
            return JsonResult.success(weiboVOS);
        }else{

            ObjectMapper objectMapper = new ObjectMapper();
            List<WeiboVO> weiboVOS = objectMapper.readValue(weiboListStr, List.class);
            return JsonResult.success(weiboVOS);
        }



    }


    @ApiOperation("查看某一篇微博")
    @ApiImplicitParam(name = "id",value = "微博的id",required = true,dataType = "int",example = "1")
    @GetMapping("/selectById")
    //@Range用于验证数字类型字段的取值范围，通过配置min和max属性来限制数值类型参数的值区间包括最小值和最大值
    public JsonResult selectById(@Range(min=1,max=99999,message = "微博id必须在1-99999之间") Integer id){
        WeiboDetailVO weiboDetailVO = iWeiboMapper.selectById(id);
        return JsonResult.success(weiboDetailVO);

    }

    @ApiOperation("根据id和name查看某一篇微博")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "微博的id",required = true,dataType = "int",example = "1"),
            @ApiImplicitParam(name = "name",value = "微博的名字",required = true,dataType = "String",example = "小博")
    })
    @GetMapping("/selectByIdAndName")
    public void selectByIdAndName(Integer id, String name){
        System.out.println(id+name);


    }
}
